package com.example.demofdfs.example.map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.util.SerializationUtils;

public class LinkedMapTest {

	public static void main(String[] args) {
		
//		test();
//		test2();
//		test3();
//		test4();
//		test5();
		test6();
	}
	
	static Map<String, String> map = instance();
	
	static void test() {
		Map<String, String> map = instance();
		
		System.out.println(map.isEmpty());
		
		
		System.out.println(map.get("a1"));
		System.out.println(map.get("a3"));
		System.out.println(map.get("a11"));
		
		
		System.out.println(map);
		System.out.println(map.size());
		System.out.println(map.isEmpty());
	}
	
	static void test2() {
		Map<String, String> map = instance();
//		Map<String, String> map = hashMap();
		Iterator<Entry<String, String>> it = map.entrySet().iterator();
		while(true) {
			if (!it.hasNext()) {
				break;
			}
			System.out.println(it.next());
		}
		
		Iterator<String> it2 = map.keySet().iterator();
		while(true) {
			if (!it2.hasNext()) {
				break;
			}
			System.out.println(it2.next());
		}
		
		Iterator<String> it3 = map.values().iterator();
		while(true) {
			if (!it3.hasNext()) {
				break;
			}
			System.out.println(it3.next());
		}
	}

	static void test3() {
		Map<String, String> map = instance();
		System.out.println(map);
		System.out.println(map.remove("a1"));
		System.out.println(map.remove("a2"));
		System.out.println(map.remove("a3"));
		System.out.println(map);
		map.putAll(hashMap());
		System.out.println(map);
	}
	
	static void test4() {
		Map<String, String> map = instance();
		System.out.println(map);
//		map.put(null, null);
//		System.out.println(map);
		System.out.println(map.containsKey("a1"));
		System.out.println(map.containsValue("dd1"));
		System.out.println(map.containsValue("dd4"));
		Iterator<String> it = map.keySet().iterator();
		while(it.hasNext()) {
			System.out.println(it.next());
			it.remove();
		}
		System.out.println(map);
	}
	
	static void test5() {
		Map<String, String> map = instance();
		System.out.println(map);
		map.clear();
		System.out.println(map);
		System.out.println(map.size());
		System.out.println(map.isEmpty());
	}
	
	static void test6() {
		System.out.println(map);
		try {
			@SuppressWarnings("unchecked")
			Map<String, String> m = (Map<String, String>) ((LinkedMap<String, String>)map).clone();
			System.out.println(m);
			System.out.println(m == map);
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		
		byte[] b = SerializationUtils.serialize(map);
		Object o = SerializationUtils.deserialize(b);
		System.out.println(o);
		Map<String, String> m2 = (Map) o;
		System.out.println(m2.size());
		
		m2 = new LinkedMap<>(hashMap());
		System.out.println(m2);
	}
	
	static Map<String, String> instance() {
		Map<String, String> map = new LinkedMap<>();
		map.put("a1", "dd1");
		map.put("a2", "dd2");
		map.put("a3", "dd3");
		map.put("a4", "dd4");
		return map;
	}
	
	static Map<String, String> hashMap() {
		Map<String, String> map = new HashMap<>();
		
		System.out.println(map.isEmpty());
		
		map.put("a111", "dd1");
		map.put("a21", "dd2");
		map.put("a23", "dd3");
		map.put("a44", "dd4");
		
		map.put("a1111", "dd4");
		return map;
	}
}
